package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.CourseDto;
import com.qk.entity.Course;
import com.qk.mapper.CourseMapper;
import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public PageResult list(CourseDto courseDto) {

        // 1.设置分页参数
        PageHelper.startPage(courseDto.getPage(), courseDto.getPageSize());

        // 2.调用mapper接口方法
        List<Course> courseList =courseMapper.selectList(courseDto);

        // 3.封装并返回分页结果
        Page<Course> p = (Page<Course>) courseList;
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    public void add(Course course) {
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.insert(course);
    }

    @Override
    public Course findById(Integer id) {
        return courseMapper.selectById(id);
    }

    @Override
    public void update(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);
    }

    @Override
    public void deleteById(Integer id) {
        courseMapper.delete(id);
    }

    @Override
    public List<Course> findSubject(Integer subject) {
        return courseMapper.selectSubject(subject);
    }

    @Override
    public List<Course> allSubject() {
        return courseMapper.selectAll();
    }


}
